package com.yanqu.road.dao.impl.platform;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.platform.PlatformReduceItemOrder;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReduceItemDaoImpl extends TempDao {

	public List<PlatformReduceItemOrder> getPlatformReduceItemOrderList(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		List<PlatformReduceItemOrder> dataList = new ArrayList<>();
		String sql = "select * from t_u_platform_reduce_item where userId =? ";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				dataList.add(initPlatformReduceItemOrder(rs));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public boolean addPlatformReduceItemOrder(PlatformReduceItemOrder data) {
		PreparedStatement call = null;
		String sql = "REPLACE INTO t_u_platform_reduce_item(orderNo,status,itemId,itemCount,orderType,userId,serverId," +
				"platformUserId,refundTime,cmd,cpOrderId,productId,cpProductId,remark,sign,createTime,modifyTime,executeType) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getOrderNo());
			call.setInt(i++, data.getStatus());
			call.setInt(i++, data.getItemId());
			call.setLong(i++, data.getItemCount());
			call.setInt(i++, data.getOrderType());
			call.setLong(i++, data.getUserId());
			call.setLong(i++, data.getServerId());
			call.setString(i++, data.getPlatformUserId());
			call.setLong(i++, data.getRefundTime());
			call.setString(i++, data.getCmd());
			call.setString(i++, data.getCpOrderId());
			call.setString(i++, data.getProductId());
			call.setString(i++, data.getCpProductId());
			call.setString(i++, data.getRemark());
			call.setString(i++, data.getSign());
			call.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
			call.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
			call.setInt(i++, data.getExecuteType());
			return call.executeUpdate() > 0;
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public PlatformReduceItemOrder getPlatformReduceItemOrder(String orderNo) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_platform_reduce_item where orderNo=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, orderNo);
			rs = call.executeQuery();
			while (rs.next()) {
				PlatformReduceItemOrder data = initPlatformReduceItemOrder(rs);
				return data;
			}
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public boolean updatePlatformReduceItemOrderStatus(PlatformReduceItemOrder data) {
		String sql = "UPDATE t_u_platform_reduce_item SET status=?,modifyTime=?,executeType=? WHERE orderNo=?";
		getDb().setConnName(DbNameStrategy.getUserDb());
		Map<Integer, DbParameter> para = new HashMap<>();
		int i = 1;
		para.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
		para.put(i++, new DbParameter(Types.DATE, new Timestamp(System.currentTimeMillis())));
		para.put(i++, new DbParameter(Types.INTEGER, data.getExecuteType()));
		para.put(i++, new DbParameter(Types.VARCHAR, data.getOrderNo()));

		return getDb().executeNoneQuery(sql, para) > -1;
	}

	private PlatformReduceItemOrder initPlatformReduceItemOrder(ResultSet rs) throws SQLException {
		PlatformReduceItemOrder data = new PlatformReduceItemOrder();
		data.setOrderNo(rs.getString("orderNo"));
		data.setStatus(rs.getInt("status"));
		data.setItemId(rs.getInt("itemId"));
		data.setItemCount(rs.getLong("itemCount"));
		data.setOrderType(rs.getInt("orderType"));
		data.setUserId(rs.getLong("userId"));
		data.setServerId(rs.getLong("serverId"));
		data.setPlatformUserId(rs.getString("platformUserId"));
		data.setRefundTime(rs.getLong("refundTime"));
		data.setCmd(rs.getString("cmd"));
		data.setCpOrderId(rs.getString("cpOrderId"));
		data.setProductId(rs.getString("productId"));
		data.setCpProductId(rs.getString("cpProductId"));
		data.setRemark(rs.getString("remark"));
		data.setSign(rs.getString("sign"));
		data.setCreateTime(new Date(rs.getTimestamp("createTime").getTime()));
		data.setModifyTime(new Date(rs.getTimestamp("modifyTime").getTime()));
		data.setExecuteType(rs.getInt("executeType"));
		return data;
	}
}
